//==================================================================
//  Copyright (C) 2006-2007  Davide Pasca
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//==================================================================
///
///
///
///
///
//==================================================================

#include "psys.h"
#include <gl/glew.h>
#include "gfxutils.h"
#include <math.h>

//==================================================================
void perspective( float fovy, float aspect, float n, float f )
{
float	xmin, xmax, ymin, ymax;

	ymax = n * (float)tan( fovy * 3.14159265f / 360.0f );
	ymin = -ymax;

	xmin = ymin * aspect;
	xmax = ymax * aspect;

	glFrustum( xmin, xmax, ymin, ymax, n, f );
}

//==================================================================
void begin_2D( int *out_vp_wp, int *out_vp_hp )
{
int	vport[4];

	glGetIntegerv( GL_VIEWPORT, vport );

	begin_2D_wh( vport[2], vport[3] );

	if ( out_vp_wp )
		*out_vp_wp = vport[2];

	if ( out_vp_hp )
		*out_vp_hp = vport[3];
}

//==================================================================
void begin_2D_wh( int w, int h )
{
	glMatrixMode( GL_PROJECTION );
	glPushMatrix();
		glLoadIdentity();
//		glOrtho( -w/2, w/2, h/2, -h/2, -1, 1 );
		glOrtho( 0, w, h, 0, 0, 1 );
//		glTranslatef( 0.5f, -0.5f, 0 );
//		glTranslatef( 0.5f, 0.5f, 0 );
//		glTranslatef( 0, h, 0 );
		glTranslatef( 0.375f, 0.375f, 0 );
//		glScalef( 1, -1, 0 );

	glMatrixMode( GL_MODELVIEW );
	glPushMatrix();
		glLoadIdentity();
}

//==================================================================
void end_2D(void)
{
	glPopMatrix();
	glMatrixMode( GL_PROJECTION );
	glPopMatrix();
	glMatrixMode( GL_MODELVIEW );
}
